package com.noahplat.datapro.service.impl;

import com.noahplat.common.utils.DateUtil;
import com.noahplat.common.utils.NoahEnum;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.datapro.mapper.*;
import com.noahplat.datapro.pojo.*;
import com.noahplat.datapro.service.DataProService;
import com.noahplat.datapro.utils.RealData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: DataProServiceImpl
 * @Description: 可穿戴设备数据service实现类
 * @Author 李景星
 * @Date 2021/3/4
 * @Version 1.0
 */
@Service
public class DataProServiceImpl implements DataProService {


    @Autowired
    private BloodProMapper bloodProMapper;
    @Autowired
    private DstatusProMapper dstatusProMapper;
    @Autowired
    private HeartProMapper heartProMapper;
    @Autowired
    private SemaphoreProMapper semaphoreProMapper;
    @Autowired
    private SiteProMapper siteProMapper;
    @Autowired
    private SpProMapper spProMapper;
    @Autowired
    private StepProMapper stepProMapper;
    @Autowired
    private TempProMapper tempProMapper;
    @Autowired
    private SleepProMapper sleepProMapper;

    /**
     * 批量查询实时数据
     * @param imeis 设备唯一标识数组
     * @param types 查询数据类型标识数组
     * @return
     */
    @Override
    public Result getReal(String[] imeis, String[] types) {
        //imei数组最多有10组
        if(imeis.length >10){
            return new Result(false, StatusCode.ERROR, "imei最多只能有10组");
        }
        ArrayList<RealData> returnData = new ArrayList<>();
        //查询数据
        for (String imei : imeis) {
            RealData realData = new RealData();
            realData.setImei(imei);
            for (String type : types) {
                if(type.equals(NoahEnum.BLOOD.getCode())){//获取实时血压
                    Blood nowBlood = new Blood();
                    try {
                        nowBlood = bloodProMapper.getNowBlood(imei);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    if(!ObjectUtils.isEmpty(nowBlood)){
                        realData.setBlood(nowBlood.getBlood());
                        realData.setBloodTime(nowBlood.getTs());
                    }
                }else if(type.equals(NoahEnum.HEART.getCode())){//获取实时心率
                    Heart nowHeart = new Heart();
                    try {
                        nowHeart = heartProMapper.getNowHeart(imei);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    if(!ObjectUtils.isEmpty(nowHeart)){
                        realData.setHeart(nowHeart.getHeart());
                        realData.setHeartTime(nowHeart.getTs());
                    }
                }else if(type.equals(NoahEnum.STATUS.getCode())){//获取当前设备状态
                    Dstatus nowStatus = dstatusProMapper.getNowStatus(imei);
                    if(!ObjectUtils.isEmpty(nowStatus)){
                        realData.setStatus(nowStatus.getStatus());
                        realData.setStatusTime(nowStatus.getTs());
                    }
                }else if(type.equals(NoahEnum.SEMAPHORE.getCode())){//获取当前信号量
                    Semaphore nowSemaphore = semaphoreProMapper.getNowSemaphore(imei);
                    if(!ObjectUtils.isEmpty(nowSemaphore)){
                        realData.setSemaphore(nowSemaphore.getSemaphore());
                        realData.setSemaphoreTime(nowSemaphore.getTs());
                    }
                }else if(type.equals(NoahEnum.SITE.getCode())){//获取当前位置坐标
                    Site nowSite = new Site();
                    try {
                        nowSite = siteProMapper.getNowSite(imei);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    if(!ObjectUtils.isEmpty(nowSite)){
                        realData.setSite(nowSite.getSite());
                        realData.setSiteTime(nowSite.getTs());
                    }
                }else if(type.equals(NoahEnum.SP.getCode())){//获取剩余电量
                    Sp nowSp = spProMapper.getNowSp(imei);
                    if(!ObjectUtils.isEmpty(nowSp)){
                        realData.setSp(nowSp.getSp());
                        realData.setSpTime(nowSp.getTs());
                    }
                }else if(type.equals(NoahEnum.STEP.getCode())){//获取当前步数
                    Date time = DateUtil.dateToFormat(new Date(),"yyyy-MM-dd HH:mm:ss");
                    Date startdate = DateUtil.toDayStartHour(time);
                    Step nowStep = new Step();
                    try {
                        nowStep = stepProMapper.getNowStep(imei,startdate);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    if(!ObjectUtils.isEmpty(nowStep)){
                        realData.setStep(nowStep.getStep());
                        realData.setStepTime(nowStep.getTs());
                    }
                }else if(type.equals(NoahEnum.TEMP.getCode())){//获取当前体温
                    Temp nowTemp = new Temp();
                    try {
                        nowTemp = tempProMapper.getNowTemp(imei);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    if(!ObjectUtils.isEmpty(nowTemp)){
                        realData.setTemp(nowTemp.getTemp());
                        realData.setTempTime(nowTemp.getTs());
                    }
                }else if(type.equals(NoahEnum.SLEEP.getCode())){//获取当前睡眠时间
                    Date time = DateUtil.dateToFormat(new Date(),"yyyy-MM-dd HH:mm:ss");
                    Date start = DateUtil.toDayStartHour(time);
                    Date startdate = DateUtil.addDateHour(start,8);
                    Date endtime = DateUtil.addDateDay(startdate,1);
                    List<Sleep> list = new ArrayList<>();
                    try {
                        list = sleepProMapper.getSleep(imei,startdate,endtime);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    int count = 0; //睡眠总时长
                    if(!ObjectUtils.isEmpty(list)){
                        for(Sleep sleep : list){
                            if(sleep.getType().equals("1") || sleep.getType().equals("2")){
                                int sleeptime = Integer.parseInt(sleep.getSleep());
                                count = count + sleeptime;
                            }
                        }
                    }
                    realData.setSleep(String.valueOf(count));
                }
            }
            returnData.add(realData);
        }
        return new Result<List<RealData>>(true, StatusCode.OK, "查询成功", returnData);
    }

    /**
     * 查询历史数据
     * @param imei
     * @param type
     * @param date
     * @return
     */
    @Override
    public Result getHis(String imei, String type, String date) {
        //日期处理
        Date startDate = DateUtil.stringToDate(date, DateUtil.PATTERN_YYYY_MM_DD);//开始日期
        Date endDate = DateUtil.addDateDay(startDate, 1);//结束时间
        //根据类型不同查询不同的历史数据集合
        if(type.equals(NoahEnum.TEMP.getCode())){
            List<Temp> hisTemp = new ArrayList<>();
            try {
                hisTemp = tempProMapper.getHisTemp(imei, startDate, endDate);
            }catch (Exception e){
                e.printStackTrace();
            }
            if(ObjectUtils.isEmpty(hisTemp)){
                return new Result(false, StatusCode.NOTFOUNDERROR, "无体温数据");
            }
            return new Result(true, StatusCode.OK, "数据查询成功", hisTemp);
        }else if(type.equals(NoahEnum.HEART.getCode())){
            List<Heart> hisHeart = new ArrayList<>();
            try {
                hisHeart = heartProMapper.getHisHeart(imei, startDate, endDate);
            }catch (Exception e){
                e.printStackTrace();
            }
            if(ObjectUtils.isEmpty(hisHeart)){
                return new Result(false, StatusCode.NOTFOUNDERROR, "无心率数据");
            }
            return new Result(true, StatusCode.OK, "数据查询成功", hisHeart);
        }else if(type.equals(NoahEnum.BLOOD.getCode())){
            List<Blood> hisBlood = new ArrayList<>();
            try {
                hisBlood = bloodProMapper.getHisBlood(imei, startDate, endDate);
            }catch (Exception e){
                e.printStackTrace();
            }
            if(ObjectUtils.isEmpty(hisBlood)){
                return new Result(false, StatusCode.NOTFOUNDERROR, "无血压数据");
            }
            return new Result(true, StatusCode.OK, "数据查询成功", hisBlood);
        }else if(type.equals(NoahEnum.STEP.getCode())){
            List<Step> hisStep = new ArrayList<>();
            try {
                hisStep = stepProMapper.getHisStep(imei, startDate, endDate);
            }catch (Exception e){
                e.printStackTrace();
            }
            if(ObjectUtils.isEmpty(hisStep)){
                return new Result(false, StatusCode.NOTFOUNDERROR, "无步数数据");
            }
            return new Result(true, StatusCode.OK, "数据查询成功", hisStep);
        }else if(type.equals(NoahEnum.SITE.getCode())){
            List<Site> hisSite = new ArrayList<>();
            try {
                hisSite = siteProMapper.getHisSite(imei, startDate, endDate);
            }catch (Exception e){
                e.printStackTrace();
            }
            if(ObjectUtils.isEmpty(hisSite)){
                return new Result(false, StatusCode.NOTFOUNDERROR, "无位置数据");
            }
            return new Result(true, StatusCode.OK, "数据查询成功", hisSite);
        }else if(type.equals(NoahEnum.SLEEP.getCode())){
            Date time = DateUtil.dateToFormat(startDate,"yyyy-MM-dd HH:mm:ss");
            Date starttime = DateUtil.addDateHour(time,8);
            Date endtime = DateUtil.addDateDay(time,1);
            List<Sleep> list = new ArrayList<>();
            try {
                list = sleepProMapper.getSleep(imei,starttime,endtime);
            }catch (Exception e){
                e.printStackTrace();
            }
            if(ObjectUtils.isEmpty(list)){
                return new Result(false,StatusCode.NOTFOUNDERROR,"无睡眠数据");
            }
            int depth = 0; //深度睡眠时长
            int shall = 0; //浅度睡眠时长
            Map<String,String> map = new HashMap<>();
            for(Sleep sleep : list){
                int sleeptime = Integer.parseInt(sleep.getSleep());
                if(sleep.getType().equals("1")){
                    depth = depth + sleeptime;
                }else if(sleep.getType().equals("2")){
                    shall = shall + sleeptime;
                }
            }
            map.put("depth",String.valueOf(depth));
            map.put("simple",String.valueOf(shall));
            return new Result(true,StatusCode.OK,"查询睡眠时间成功",map);
        }else {
            return new Result(false, StatusCode.ERROR, "不支持该类型历史数据查询");
        }
    }

    /**
     * 工地项目查询位置数据
     */
    @Override
    public Result selectSite(List<String> imeis){
        List<Site> siteList = new ArrayList<>();
        try {
            siteList = siteProMapper.selectSite(imeis);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
        return new Result(true,StatusCode.OK,"查询成功",siteList);
    }

}
